package reflect;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * 自动调用Person中名字含有S的无参方法
 */
public class reflectDemo7 {
    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, InvocationTargetException {
        Class cls=Class.forName("reflect.Person");
        Object o=cls.newInstance();

        Method[] methods=cls.getDeclaredMethods();
        for (Method method : methods) {
            String methodName=method.getName();
            //名字含有S 并且参数的个数为0
            if (methodName.toLowerCase().contains("s") && method.getParameterCount()==0){
                System.out.println("自动调用方法:"+methodName+"()");
                method.invoke(o);
            }
        }
    }
}
